package com.qnzw.zjmy.home.ui.activity.agent;


import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.android.volley.Request;
import com.qnzw.zjmy.R;
import com.qnzw.zjmy.config.UrlConfig;
import com.qnzw.zjmy.home.ui.adapter.TheChartsAdapter;
import com.qnzw.zjmy.home.ui.model.statistics.RankingList;
import com.qnzw.zjmy.refresh.LoadingFooter;
import com.qnzw.zjmy.refresh.xRecyclerView;
import com.qnzw.zjmy.volley.BaseActivity;
import com.qnzw.zjmy.volley.state.HUCState;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;


public class TheChartsActivity extends BaseActivity {
    private xRecyclerView xRecyclerView;
    private TheChartsAdapter theChartsAdapter;
    private List<RankingList> rankingLists = new ArrayList<>();
    private TextView tvCount, tvMoney, tvNickName, tvCode, tvContent;
    private boolean refreshFlag = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_the_charts);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("排行榜");
        tvCode.setText("ID：" + getSharedPreferences("id"));
        tvNickName.setText(getSharedPreferences("nickname"));
        theChartsAdapter = new TheChartsAdapter(this, rankingLists, R.layout.item_income);
        xRecyclerView.setAdapter(this, theChartsAdapter, new LinearLayoutManager(this));
        xRecyclerView.setOnRefreshListener(this);
        Intent intent = getIntent();
        // 其他数据类型可以使用 getIntExtra, getBooleanExtra 等方法
        String tag = intent.getStringExtra("tag");
        switch (Integer.parseInt(tag)) {
            case HUCState.HUC_REQEUST_CODE_1:
                tvContent.setText("总收入");
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                tvContent.setText("本月收入");
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                tvContent.setText("今日收入");
                break;
        }
        pageVo.setStatus(tag);
    }

    @Override
    public void initView() {
        super.initView();
        xRecyclerView = findViewById(R.id.xrecyclerView);
        tvCount = findViewById(R.id.tv_count);
        tvContent = findViewById(R.id.tv_content);
        tvMoney = findViewById(R.id.tv_money);
        tvNickName = findViewById(R.id.tv_nickname);
        tvCode = findViewById(R.id.tv_code);
    }

    /**
     * 下拉刷新
     *
     * @param refreshLayout
     */
    @Override
    public void refresh(SwipeRefreshLayout refreshLayout) {
        refreshFlag = false;
        onStart();
    }

    /**
     * 上拉加载
     *
     * @param recyclerView
     */
    @Override
    public void loadMore(RecyclerView recyclerView) {
        xRecyclerView.refreshComplete(LoadingFooter.State.TheEnd, "");
    }

    private void getInitData() {
        hucRequestInterfase.executeStr(this, RankingList.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_USER_THE_CHARTS,
                hucParameInterfase.getRequestParameter(pageVo), false);
        loading();
    }


    @Override
    public void onSuccessfulStrType(int inputType, Object obj) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (obj instanceof List) {
                    List<RankingList> rankingListList = (List<RankingList>) obj;
                    if (refreshFlag) {
                        if (rankingListList == null || rankingListList.size() == 0) {
                            if (rankingListList.size() == 0) {
                                xRecyclerView.refreshComplete(LoadingFooter.State.NoData, "");
                            } else {
                                xRecyclerView.refreshComplete(LoadingFooter.State.TheEnd, "");
                            }
                        } else {
                            for (RankingList rankingList : rankingListList) {
                                rankingLists.add(rankingList);
                            }
                            theChartsAdapter.notifyDataSetChanged();
                            xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                        }
                    } else {
                        rankingLists.clear();
                        for (RankingList rankingList : rankingListList) {
                            rankingLists.add(rankingList);
                        }
                        theChartsAdapter.notifyDataSetChanged();
                        xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                    }
                    tvCount.setText(String.valueOf(rankingLists.size()));
                    int sum = 0;
                    for (RankingList rankingList : rankingLists) {
                        int money = Integer.parseInt(rankingList.getMoney());
                        sum += money;
                    }
                    BigDecimal money = BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(10000), 2, RoundingMode.DOWN);
                    tvMoney.setText(String.valueOf(money));
                }
                break;
            default:
                break;
        }
        loadDismiss();
    }

    @Override
    protected void onStart() {
        pageVo.setToken(getSharedPreferencesToken("token"));
        super.onStart();
        refreshFlag = false;
        pageVo.setCurrent(1 + "");
        pageVo.setSize(10000 + "");
        getInitData();
    }
}